const redis = require('redis')
const {
  REDIS_HOST,
  REDIS_PORT,
} = require('../../config')

const redisClient = redis.createClient({
  host: REDIS_HOST,
  port: REDIS_PORT,
  // password: REDIS_PASSWORD,
  retry_strategy: (options) => { // eslint-disable-line
    // if (options.error && options.error.code === 'ECONNREFUSED') {
    //   // End reconnecting on a specific error and flush all commands with
    //   // a individual error
    //   return new Error('The server refused the connection')
    // }
    // if (options.total_retry_time > 1000 * 60 * 60) {
    //   // End reconnecting after a specific timeout and flush all commands
    //   // with a individual error
    //   return new Error('Retry time exhausted')
    // }
    // if (options.attempt > 10) {
    //   // End reconnecting with built in error
    //   return undefined
    // }
    // // reconnect after
    // return Math.min(options.attempt * 100, 3000)
    return undefined
  },
})

redisClient.on('error', (error) => {
  console.log(error.message) // eslint-disable-line
})

redisClient.set('TESTING_KEY', 'TESTING_VALUE', redis.print)
redisClient.get('TESTING_KEY', (error, result) => {
  if (error) {
    console.log(error.message) // eslint-disable-line
  }
  console.log('GET result ->', result) // eslint-disable-line
})

redisClient.addRecord = (key, value, expire) => {
  redisClient.set(key, value, 'EX', expire)
}

redisClient.getRecord = (key) => {
  return new Promise((resolve) => {
    redisClient.get(key, (error, result) => {
      if (error) {
        return resolve(null)
      }
      return resolve(result)
    })
  })
}

module.exports = redisClient
